package com.jumper.mydemo.statics;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.HierarchicalBeanFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.jumper.mydemo.my.MyBean;

public class HierachicalBeanFactoryDemo {
    @SuppressWarnings("resource")
    public static void run() {
        AnnotationConfigApplicationContext parentContext = new AnnotationConfigApplicationContext(MyBean.class);
        AnnotationConfigApplicationContext childContext = new AnnotationConfigApplicationContext();
        childContext.setParent(parentContext);

        HierarchicalBeanFactory childHierarchicalBeanFactory = childContext.getBeanFactory();
        System.out.println("在子级BeanFactory中获取Bean:" + childHierarchicalBeanFactory.getBean(MyBean.class));

        HierarchicalBeanFactory parentHierarchicalBeanFactory = parentContext.getBeanFactory();
        System.out.println("在父级BeanFactory中获取Bean:" + parentHierarchicalBeanFactory.getBean(MyBean.class));

        BeanFactory parentBeanFactory = childHierarchicalBeanFactory.getParentBeanFactory();
        System.out.println("获取父级BeanFactory:" + parentBeanFactory);

        boolean containsLocalBean = childHierarchicalBeanFactory.containsLocalBean("myBean");
        System.out.println("判断本地BeanFactory是否包含指定Bean:" + containsLocalBean);

        boolean containsBean = childHierarchicalBeanFactory.containsBean("myBean");
        System.out.println("判断整个BeanFactory是否包含指定Bean:" + containsBean);
    }
}
